/*******************************************************************/
/*    Copyright (c) 1999-2013 by Spatial Corp.                     */
/*    All rights reserved.                                         */
/*******************************************************************/

//------------------------------------------------------------------------------------------------

#include "SPAIDocument.h"
#include "SPAIConverter.h"
#include "SPAIFile.h"
#include "SPAIResult.h"
#include "SPAIValue.h"

#include "SPAISystemInitGuard.h"

// SPAXGeneric
#include "SPAIParasolidDocument.h"

#include "SPAIArgumentParsingParasolid.h"

//#include "PsSession.h"
#include "parasolid_kernel.h"

//------------------------------------------------------------------------------------------------

int main(int argc, char *argv[])
{
	SPAISystemInitGuard initGuard;

	// SKY 6/1/06 - Licensing project
	unlock_license();

	int err = 1;
	if ( ( err = getArgs(argc, argv) ) !=0)
		return err;

	SPAIResult result=SPAI_S_OK;

	StartParasolid();

	int partCount = 0;
	PK_PART_t* pParts = NULL;

	PK_ERROR_code_t  status = PK_ERROR_wrong_format;

	PK_PART_receive_o_t psReadOpt;
	PK_PART_receive_o_m(psReadOpt);

	psReadOpt.attdef_mismatch = PK_ATTDEF_mismatch_fail_c;
	psReadOpt.o_t_version =1;

	PK_transmit_format_t formats[] = {PK_transmit_format_text_c, PK_transmit_format_binary_c, PK_transmit_format_neutral_c, PK_transmit_format_applio_c};

#ifdef _WINDOWS_SOURCE
	PK_UCHAR_t * unicodeFileName = ( PK_UCHAR_t*)pszInputFile;
#else
	char asciiFileName[MAX_FILE_NAME_CHARS]="";
	int convertedChars=wcstombs( asciiFileName, pszInputFile, MAX_FILE_NAME_CHARS );
	//	const SPAXMILUniCodeChar * fileName = ( SPAXMILUniCodeChar*)fileCharName;
	if ( -1 == convertedChars)
	{
		result=SPAI_E_FAIL;
		return result;
	}
#endif

	for ( int trial = 0; ( ( trial < 4 ) && status != PK_ERROR_no_errors); trial++ )
	{
		psReadOpt.transmit_format = formats[ trial];
#ifdef _WINDOWS_SOURCE
		status = PK_PART_receive_u( unicodeFileName, &psReadOpt, &partCount, &pParts );
#else
		status = PK_PART_receive( asciiFileName, &psReadOpt, &partCount, &pParts );
#endif
	}

	SPAIParasolidDocument src(partCount, pParts);

	SPAIDocument dst(pszOutputFile);
	if (strlen(pszOutputFormat)>0)
		dst.SetType(pszOutputFormat);

	SPAIFile console(1);

	SPAIConverter converter;
	result&=converter.StartLog(console);
	result&=converter.Convert(src, dst);
	result&=converter.StopLog(console);

	StopParasolid();

	printf("%s \n", result.GetMessage() );

	return result;
}

//------------------------------------------------------------------------------------------------

void printHelp()
{
	printf("Usage : ParasolidExport -i file -o file [-oformat format]\n");
	printf("Options :\n");
	printf("-i        : input (generic) file\n");
	printf("-o        : output file\n");
	printf("-oformat  : output format\n");
}

//------------------------------------------------------------------------------------------------
